from django.shortcuts import get_object_or_404, render
from django.contrib.auth.decorators import login_required
from datetime import date
from django.http import HttpResponseRedirect
from common.util.fiboapi import *
from django.utils.translation import ugettext as _
import re


@login_required(redirect_field_name=None)
def sms_list(request):
    fromdate= request.GET.get("fromdate")
    todate= request.GET.get("todate")
    status= request.GET.get("status")
    page= request.GET.get("page")
 
    if(not fromdate):
        fromdate=""
        
    if(todate==None or todate==""):
        todate=""
            
    if(status==None or status=="none"):
        status=""
     
    if(page==None or page=="none"):
        page=0
 
  
    sms_list=str(smsGetClientSMSListOfPage(fromdate,todate,status,page))
    string=sms_list.replace("\n","")
    regex_data = re.compile("<SMSLIST>(.+?)<\/SMSLIST>")
    regex_content = re.compile("<.+?>(.+?)<\/.+?>")
    regex_tag = re.compile("<([^<\/]+)>")
    
    data=[]
    regex_data_list=regex_data.findall(string)
    for data_list in regex_data_list:
        regex_content_list=regex_content.findall(data_list)
        regex_tag_list=regex_tag.findall(data_list)
        #list = [{t[0]:t[1]} for t in zip(regex_tag_list, regex_content_list)]
        #data.append(list)
        list = dict(zip(regex_tag_list, regex_content_list)) 
        data.append(list)


    sms_page=GetTotalPageOfClientSMS(fromdate,todate,status)+1
    
    status=[_("luca_statistic_sending"),_("luca_statistic_sent"),_("luca_statistic_failed"),_("luca_statistic_deleted")]
    context = {'listdata':data,'sms_page':sms_page,'status':status}
    return render(request, 'sms_list.html',context)